package com.company.genealogy.web.controller;

import com.company.genealogy.common.result.ApiResponse;
import com.company.genealogy.web.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.company.genealogy.common.constant.ApiCodeEnum;

@RestController
@RequestMapping("/api/v1/upload")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    @PostMapping("/member-photo")
    public ApiResponse<String> uploadMemberPhoto(@RequestParam("file") MultipartFile file) {
        try {
            String filePath = fileUploadService.uploadMemberPhoto(file);
            return ApiResponse.success(ApiCodeEnum.SUCCESS, filePath);
        } catch (IllegalArgumentException e) {
            return ApiResponse.fail(e.getMessage());
        } catch (Exception e) {
            return ApiResponse.fail("文件上传失败: " + e.getMessage());
        }
    }

    @PostMapping("/spouse-photo")
    public ApiResponse<String> uploadSpousePhoto(@RequestParam("file") MultipartFile file) {
        try {
            String filePath = fileUploadService.uploadSpousePhoto(file);
            return ApiResponse.success(ApiCodeEnum.SUCCESS, filePath);
        } catch (IllegalArgumentException e) {
            return ApiResponse.fail(e.getMessage());
        } catch (Exception e) {
            return ApiResponse.fail("文件上传失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/member-photo")
    public ApiResponse<Void> deleteMemberPhoto(@RequestParam String filePath) {
        boolean success = fileUploadService.deleteFile(filePath);
        if (success) {
            return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
        } else {
            return ApiResponse.fail("文件删除失败");
        }
    }

    @DeleteMapping("/spouse-photo")
    public ApiResponse<Void> deleteSpousePhoto(@RequestParam String filePath) {
        boolean success = fileUploadService.deleteFile(filePath);
        if (success) {
            return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
        } else {
            return ApiResponse.fail("文件删除失败");
        }
    }
} 